package com.lm.survey.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lm.survey.util.Config;

/**
 * Servlet implementation class GetMP3FileServlet
 */
public class GetMP3FileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.setContentType("audio/x-mpeg");
		//
		response.setContentType("audio/x-pn-realaudio");
		String str  = request.getRequestURI();
		int index = str.indexOf("getfile");
		String fileName = str.substring(index+7);
		FileInputStream ins = new FileInputStream(Config.instance().getStringProperty("MP3FilePath")+fileName);
		
		
		byte bt[] = new byte[1024];
		int c;
		while ( (c = ins.read(bt)) > 0) {
		response.getOutputStream().write(bt, 0, c);
		}
		response.getOutputStream().close();
		ins.close();
		
	}

}
